package com.tronsis.distribution.activity.me;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tronsis.distribution.R;
import com.tronsis.distribution.activity.MainActivity;
import com.tronsis.distribution.activity.goods.GoodsDetailActivity;
import com.tronsis.distribution.bean.PayeeMethodBean;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.http.ListBean;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.util.ToastUtil;
import com.tronsis.library.widget.ConfirmDialog;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PayeeAccountActivity extends ToolBarBaseActivity {


    @BindView(R.id.rv_payee_account)
    RecyclerView mRvPayeeAccount;

    private CommonAdapter<PayeeMethodBean> mAdapter;
    private List<PayeeMethodBean> mPayeeAccounts = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payee_account);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.payee_account));
        initData();
        getPayeeAccount();
    }


    void initData() {
        mAdapter = new CommonAdapter<PayeeMethodBean>(mContext, mPayeeAccounts, R.layout.item_payee_account) {
            @Override
            protected void convert(ViewHolder holder, PayeeMethodBean payeeMethodBean, int position) {

                final String account = payeeMethodBean.getAccount();
                final String bank = payeeMethodBean.getBank();
                final String payeeId = payeeMethodBean.getId();
                final String name = payeeMethodBean.getName();
                holder.setText(R.id.tv_bank, bank);
                holder.setText(R.id.tv_name, name);
                holder.setText(R.id.tv_account, account);
                holder.setOnClickListener(R.id.tv_edit, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ;
                        showDialog(account, bank, payeeId, name);

                    }
                });

                holder.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        new ConfirmDialog(mContext, new ConfirmDialog.OnBottomButtonClickListener() {
                            @Override
                            public void onLeftButtonClick(View view) {

                            }

                            @Override
                            public void onRightButtonClick(View view) {
                                deletePayeeAccount(payeeId);
                            }
                        }).showDialog("确认删除？", "取消", "确定");

                    }
                });
            }

        };

        int height = DisplayUtil.getInstance(mContext).dp2px(1);
        int color = getResources().getColor(R.color.table_line);
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvPayeeAccount.addItemDecoration(decoration);
        mRvPayeeAccount.setLayoutManager(new LinearLayoutManager(mContext));
        mRvPayeeAccount.setAdapter(mAdapter);
    }


    private void showDialog(final String account, final String bank, final String payeeId, final String name) {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_payee, null, false);
        final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();

        Button btnOk = view.findViewById(R.id.btn_confirm);
        ImageButton btnClose = view.findViewById(R.id.mbt_close);
        TextView tvPayeeAccountTittle = view.findViewById(R.id.tv_payee_account_tittle);
        final EditText etAccount = view.findViewById(R.id.et_account);
        final EditText etBank = view.findViewById(R.id.et_bank);
        final EditText etName = view.findViewById(R.id.et_name);
        etName.setText(name);
        etAccount.setText(account);
        etBank.setText(bank);
        if (payeeId == null) {
            tvPayeeAccountTittle.setText(getString(R.string.add_payee_account));
        } else {
            tvPayeeAccountTittle.setText(getString(R.string.edit_payee_account));
        }
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String bank = etBank.getText().toString();
                String name = etName.getText().toString();
                String account = etAccount.getText().toString();
                if (TextUtils.isEmpty(bank)) {
                    ToastUtil.showMessage(mContext, "请输入账号名称");
                    return;
                }

                if (TextUtils.isEmpty(name)) {
                    ToastUtil.showMessage(mContext, "请输入收款人");
                    return;
                }

                if (TextUtils.isEmpty(account)) {
                    ToastUtil.showMessage(mContext, "请输入账号");
                    return;
                }
                if (payeeId == null) {
                    addPayeeAccount(account, bank, name, dialog);
                } else {
                    updatePayeeAccount(account, bank, name, payeeId, dialog);
                }
            }
        });

        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    private void deletePayeeAccount(final String payeeId) {
        try {
            UserBizFactory.createBiz().deletePayeeMethod(this, payeeId, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    getPayeeAccount();

                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    private void updatePayeeAccount(final String account, final String bank, final String name, final String payeeId, final AlertDialog dialog) {
        try {
            UserBizFactory.createBiz().updatePayeeMethod(this, account, bank, name, payeeId, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    dialog.dismiss();
                    getPayeeAccount();

                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    private void addPayeeAccount(final String account, final String bank, final String name, final AlertDialog dialog) {
        try {
            UserBizFactory.createBiz().addPayeeMethod(this, account, bank, name, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    dialog.dismiss();
                    getPayeeAccount();

                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }


    private void getPayeeAccount() {
        try {
            UserBizFactory.createBiz().getPayeeMethod(this, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    List<PayeeMethodBean> payeeMethodList = (List<PayeeMethodBean>) data;
                    mPayeeAccounts.clear();
                    mPayeeAccounts.addAll(payeeMethodList);
                    mAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    @OnClick(R.id.btn_add)
    void addClick() {
        showDialog(null, null, null, "");
    }

}
